package com.wuyou.simple;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 2287. 重排字符形成目标字符串
 */
public class RearrangeCharacters {

    public static void main(String[] args) {String s = "ilovecodingonleetcode", target = "code";
//        System.out.println(solution_2(s, target));
//        System.out.println((int)'a');
        System.out.println((int) '!');
        System.out.println((int) '@');
        System.out.println((int) '#');
        System.out.println((int) '$');
        System.out.println((int) '%');
        System.out.println((int) '^');
        System.out.println((int) '&');
        System.out.println((int) '*');
        System.out.println((int) '(');
        System.out.println((int) ')');
        System.out.println((int) '-');
        System.out.println((int) '+');
    }


    /**
     * 执行用时：0 ms 在所有 Java 提交中击败了100.00% 的用户
     * 内存消耗：39.2 MB, 在所有 Java 提交中击败了 96.83% 的用户
     * @param s
     * @param target
     * @return
     */
    public static int solution_2(String s, String target) {
        if (s.length() == 0 || s.length() < target.length() || target.length() == 0) return 0;
        int[] sArr = new int[26];
        int[] targetArr = new int[26];
        for (int i = 0; i < s.length(); i++) {
            sArr[s.charAt(i) - 97] += 1;
            if(i < target.length()){
                targetArr[target.charAt(i) - 97] += 1;
            }
        }
        int resMin = sArr[target.charAt(0) - 97] / targetArr[target.charAt(0) - 97];
        for (int i = 0; i < target.length(); i++) {
            resMin = Math.min(resMin, sArr[target.charAt(i) - 97] / targetArr[target.charAt(i) - 97]);
        }
        return resMin;
    }

    /**
     * 执行用时：1 ms  在所有 Java 提交中击败了 40.21% 的用户
     * 内存消耗：39.3 MB, 在所有 Java 提交中击败了 85.19% 的用户
     * @param s
     * @param target
     * @return
     */
    public static int solution_1(String s, String target) {
        if (s.length() == 0 || s.length() < target.length() || target.length() == 0) return 0;
        Map<Character, Integer> map = new HashMap<>();
        Map<Character, Integer> map1 = new HashMap<>();
        char[] chars = s.toCharArray();
        char[] chars1 = target.toCharArray();

        for (int i = 0; i < chars.length; i++) {
            map.put(chars[i], map.getOrDefault(chars[i], 0) + 1);
        }
        for (int i = 0; i < chars1.length; i++) {
            map1.put(chars1[i], map1.getOrDefault(chars1[i], 0) + 1);
        }
        int resMin = map.getOrDefault(chars1[0], 0);
        for (int i = 0; i < chars1.length; i++) {
            Integer count = map.getOrDefault(chars1[i], 0);
            Integer count1 = map1.getOrDefault(chars1[i], 0);
            resMin = Math.min(resMin, count / count1);
        }
        return resMin;
    }
}
